﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Specifications;
using AppFramework.DinosaursParkMes.BOM;
using AppFramework.DinosaursParkMes.CodingRuleModel;
using AppFramework.DinosaursParkMes.Execute.Dto;
using AppFramework.DinosaursParkMes.Process;
using AppFramework.DinosaursParkMes.Product;
using CommunityToolkit.HighPerformance;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppFramework.DinosaursParkMes.Execute
{
    public class ExecuteScheduleAppService : AppFrameworkAppServiceBase, IExecuteScheduleAppService
    {
        private readonly IRepository<ProductWorkModels> _ProductWorkModels;
        private readonly IRepository<Productinfo> _Productinfo;
        private readonly IRepository<CraftProcessModel> _CraftProcessModel;
        private readonly IRepository<ProcessModel> _ProcessModel;
        private readonly IRepository<AppFramework.DinosaursParkMes.ProductionPlanningModel.ProductionPlanning> _ProductionPlanning;
        private readonly IRepository<BillOfMaterial> _billof;
        private readonly IRepository<CraftRouteModel> _CraftRouteModel;
        public ExecuteScheduleAppService(IRepository<ProductWorkModels> productWorkModels, IRepository<Productinfo> productinfo, IRepository<CraftProcessModel> craftProcessModel, IRepository<ProcessModel> processModel, IRepository<ProductionPlanningModel.ProductionPlanning> productionPlanning, IRepository<BillOfMaterial> billof, IRepository<CraftRouteModel> craftRouteModel)
        {
            _ProductWorkModels = productWorkModels;
            _Productinfo = productinfo;
            _CraftProcessModel = craftProcessModel;
            _ProcessModel = processModel;
            _ProductionPlanning = productionPlanning;
            _billof = billof;
            _CraftRouteModel = craftRouteModel;
        }

        //public Task<List<ExecuteDto>> GetExecutes()
        //{
        //    throw new NotImplementedException();
        //}

        /// <summary>
        /// 获取执行进度列表数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ListResultDto<ExecuteDto>> GetExecutes()
        {
            //var plan = _ProductionPlanning.GetAll().FirstOrDefault();
            //var bom=_billof.GetAll().FirstOrDefault();
            //var route = _CraftProcessModel.GetAll().FirstOrDefault();
            //var product = _ProductWorkModels.GetAll().Where(x=>x.ExplainNumber==plan.Planningnumber && plan.BOMNumber==bom.Numberbom && bom.Processnumber==route.Craftcode).ToList();
            //return ObjectMapper.Map<List<ExecuteDto>>(product);
            //var route = _CraftProcessModel.GetAll().FirstOrDefault();
            //var bom = _billof.GetAll().Where(x=>x.Processnumber==route.Craftcode).FirstOrDefault();
            //var plan = _ProductionPlanning.GetAll().Where(x=>x.BOMNumber==bom.Numberbom).FirstOrDefault();
            //var product = _ProductWorkModels.GetAll().Where(x=>x.ExplainNumber==plan.Planningnumber).ToList();
            //return ObjectMapper.Map<List<ExecuteDto>>(product);
            
            
            var list = from x in _ProductWorkModels.GetAll()
                       join y in _ProductionPlanning.GetAll() on x.ExplainNumber equals y.Planningnumber
                       join z in _billof.GetAll() on y.BOMNumber equals z.Numberbom
                       join a in _CraftProcessModel.GetAll() on z.Processnumber equals a.Craftcode
                       join b in _CraftRouteModel.GetAll() on a.Craftcode equals b.Routecoded
                       select new ExecuteDto
                       {
                           WorkOrderNumber = x.WorkOrderNumber,
                           State = x.State,
                           ProductNumber = x.ProductNumber,
                           ProductName = x.ProductName,
                           Specification=x.Specification,
                           Routename=b.Routename,
                           Processcode=a.Craftcode,
                           PlanNumber=x.ExplainNum,
                           DisqualificationNumber=x.Id,
                           QualifiedNumber=x.Id+3,
                           RealQuantity=x.Id+x.Id+3, 
                       };
            return new ListResultDto<ExecuteDto>
            {
                Items = list.ToList(),
            };
        }
    }
}
